<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-07-05 13:33
 *
 * Version: 2.0
 */

namespace App\Modules\DataCenter\Repositories;



use Illuminate\Support\Facades\DB;

class StatisticsRepository
{

	public function  getStoreOrderBusinessData($dateList,$storeId){
		$sql = 'select  s.`type`,count(i.`id`) as nums,sum(o.`real_price`) as amount ,sum(o.`profit`) as profit 
from `hx_order_item` as i 
left join `hx_order` as o on i.`order_id`= o.`id` and o.`status`= 2
left join `hx_store_service` as s on i.`service_id` = s.`id` and s.`deleted_at` is null
where o.`store_id`="'.$storeId.'" and  FROM_UNIXTIME(o.`finish_time`,\'%Y-%m-%d\') >="'.$dateList['end_date'].'" and  FROM_UNIXTIME(o.`finish_time`,\'%Y-%m-%d\') <="'.$dateList['start_date'].'"  group by s.`type`  UNION select \'sp\' type, count(g.id) as nums,sum(g.`real_price`) as amount ,sum( g.`real_price` - g.`buying_sum`- g.`royalty`) as profit  from `hx_goods_order` as g where g.`store_id` = "'.$storeId.'" and  date(g.`created_at`) >= "'.$dateList['end_date'].'"  and date(g.`created_at`) <= "'.$dateList['start_date'].'"  and g.`status`=1  UNION select \'bk\' type, count(m.`id`) as nums, sum(m.`real_amount`) as amount, sum(case when (r.`amount` is null) then m.`real_amount` else  (m.`real_amount` - r.`amount`) end) as profit from `hx_member_card_flow` as m  left join `hx_royalty` as r on m.`id`=r.`item_id` and r.`type`= 2   where (m.`type`=1 or m.`type`=2) and m.`real_amount` >0 and date(m.`created_at`) >= "'.$dateList['end_date'].'"  and date(m.`created_at`) <= "'.$dateList['start_date'].'"  and m.`store_id`="'.$storeId.'"';
		return DB::select($sql);
	}




}